<?php


namespace app\task;


use app\api\logic\GoodsSearchLogic;
use support\constants\RedisKey;
use support\contract\ITask;
use support\Redis;
use Workerman\Crontab\Crontab;

/**
 * 商品搜索引擎数据同步,用于比如订单业务操作了销量等信息 需要同步到meilisearch
 * Class GoodsSearchTask
 * @package app\task
 */
class GoodsSearchSyncTask implements ITask
{
    public function start()
    {
        //定时任务保底执行,防止同步队列消费失败的情况
        // 每5分钟执行一次
        new Crontab('0 */5 * * * *', function () {
            $goodsIds = Redis::sMembers(RedisKey::SEARCH_SYNC);
            if ($goodsIds) {
                GoodsSearchLogic::sync($goodsIds);
                Redis::sRem(RedisKey::SEARCH_SYNC, ...$goodsIds);
            }
        });
    }
}